import pandas as pd
import json


def pre_process():
  # 输入输出路径
  input_csv = "0208-0313.csv"      # 替换为你的CSV路径
  output_jsonl = "0208-0313output.jsonl"  # 输出的JSONL文件

  def build_prompt(text):
    return f"{text}。根据前面的文字，判断有没有体现出客户的意向,仅输出0（无意向），1（有意向），2（无法判断），不要任何解释"

  # 读取CSV文件
  try:
    df = pd.read_csv(input_csv)
  except FileNotFoundError:
    print(f"错误：文件 {input_csv} 不存在")
    exit()

  # 检查是否存在content列
  if "summary" not in df.columns:
    print("错误：CSV文件中缺少 'summary' 列")
    exit()

  df = df.dropna()
  df.to_csv('2028-0313_clear.csv',index= False)
  # 生成JSONL文件
  with open(output_jsonl, "w", encoding="utf-8") as f:
    for idx, row in enumerate(df.itertuples(), start=1):
      # 跳过空内容
      if pd.isna(row.summary) or not str(row.summary).strip():
        continue

      # 构建JSON结构
      payload = {
        "custom_id": f"request-{idx}",
        "method": "POST",
        "url": "/v1/chat/completions",
        "body": {
          "model": "qwen-max",
          "messages": [
            {
              "role": "user",
              "content": build_prompt(str(row.summary).strip())  # 确保转换为字符串并去除首尾空格
            }
          ]
        }
      }
      # 写入文件
      f.write(json.dumps(payload, ensure_ascii=False) + "\n")

  print(f"生成完成！有效数据量：{len(df)} 条，输出文件：{output_jsonl}")

def post_process(download_jsonl, result_file):
  # download_jsonl = "812d8d05-4dec-4470-b16d-3a5d84f12e0d_1741921890232_success.jsonl"  # 输出的JSONL文件
  import json
  import csv
  columns = ["custom_id",
             "model",
             "request_id",
             "status_code",
             "error_code",
             "error_message",
             "created",
             "content",
             "usage"]

  def dict_get_string(dict_obj, path):
    obj = dict_obj
    try:
      for element in path:
        obj = obj[element]
      return obj
    except:
      return None

  with open(download_jsonl, "r") as fin:
    with open(result_file, 'w', encoding='utf-8') as fout:
      rows = [columns]
      for line in fin:
        request_result = json.loads(line)
        row = [dict_get_string(request_result, ["custom_id"]),
               dict_get_string(request_result, ["response", "body", "model"]),
               dict_get_string(request_result, ["response", "request_id"]),
               dict_get_string(request_result, ["response", "status_code"]),
               dict_get_string(request_result, ["error", "error_code"]),
               dict_get_string(request_result, ["error", "error_message"]),
               dict_get_string(request_result, ["response", "body", "created"]),
               dict_get_string(request_result, ["response", "body", "choices", 0, "message", "content"]),
               dict_get_string(request_result, ["response", "body", "usage"])]
        rows.append(row)
      writer = csv.writer(fout)
      writer.writerows(rows)

def merge_result(origin_file, result_file):
  df_summary = pd.read_csv(origin_file )
  df_summary= df_summary.dropna()
  df_senti = pd.read_csv(result_file)
  print(f'df_summary的大小为{len(df_summary)}')
  print(f'df_senti的大小为{len(df_senti)}')
  df_senti = df_senti[['content']]
  result = pd.concat([df_summary, df_senti], axis=1)
  # result['label']=result['content'].apply(lambda x: str(int(x)))
  print(result.head(10))
  print(result.dtypes)
  result.to_csv('merge_re.csv', index = False)


if __name__ == '__main__':
  # pre_process()
  # post_process(download_jsonl='0208_0313_result.jsonl', result_file='0208_0313_result.csv')
  merge_result(origin_file='2028-0313_clear.csv', result_file='0208_0313_result.csv')